﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using ProtoBuf.Message;
using Dict.Blo;
using Dict.DataModel;

namespace UI
{
    public class UICopyBossCook : MonoBehaviour
    {
        public GameObject wantedBossRoot;
        public GameObject cookBonusRoot;
        public GameObject bossIconRoot;
        public UILabel labBossName;
        public UILabel labBonusValue;
        public UILabel labBonusTime;
        public GameObject heroIconPref;

        RebelInfo bossInfo;
        List<ContinuedEffect> cookEffectList;
        float delateTime = 0;

        public void RefreshBossCook()
        {
            wantedBossRoot.SetActive(false);
            cookBonusRoot.SetActive(false);
            
            CSGetRebels msg = new CSGetRebels();
            NetAPI.SendHttp(OpDefine.CSGetRebels, msg, OnMsgRecv);
        }

        void OnMsgRecv(NetWork.Packets.Packet packet, bool success)
        {
            if (success)
            {
                SCGetRebels rmsg = packet.kBody as SCGetRebels;
                foreach(var boss in rmsg.rebels)
                {
                    if(boss.finder == StaticData.playerData.playerId)
                    {
                        bossInfo = boss;
                        break;
                    }
                }
            }
            
            CSCookingEffectList msg = new CSCookingEffectList();
            NetAPI.SendHttp(OpDefine.CSCookingEffectList, msg, OnEffectListRecv);
        }

        void OnEffectListRecv(NetWork.Packets.Packet packet, bool success)
        {
            if (success)
            {
                SCCookingEffectList rmsg = packet.kBody as SCCookingEffectList;
                cookEffectList = rmsg.effectList;
                RefreshOnRecv();
            }
        }

        public void OnBossClick()
        {
            PageManager.instance.OpenPage("MercenaryTaskPage", "tab=1");
        }

        void RefreshOnRecv()
        {
            if (cookEffectList != null && cookEffectList.Count > 0)
            {
                cookBonusRoot.SetActive(true);
                string effectStr = "";
                long endTime = 0;
                foreach (var effect in cookEffectList)
                {
                    if (effectStr != "")
                        effectStr += "\n";
                    switch(effect.effectId)
                    {
                        case "extraExp":
                            effectStr += TableTextBlo.GetText("exp") + "+" + effect.effectValue * 100 + "%";
                            break;
                        case "extraCoin":
                            effectStr += TableTextBlo.GetText("coin") + "+" + effect.effectValue * 100 + "%";
                            break;
                        case "extraCoinExp":
                            effectStr += TableTextBlo.GetText("exp") + "+" + effect.effectValue * 100 + "%";
                            effectStr += "\n";
                            effectStr += TableTextBlo.GetText("coin") + "+" + effect.effectValue * 100 + "%";
                            break;
                    }
                    endTime = effect.effectTime;
                }
                long curTime = ServerTimerTool.CurrentUTCJavaTime();
                System.DateTime time1 = ServerTimerTool.Java2CSTime(endTime);
                System.DateTime time2 = ServerTimerTool.Java2CSTime(curTime);
                System.TimeSpan time = time1.Subtract(time2);
                string timestr = "";
                if (time.Minutes > 0)
                    timestr = time.Minutes.ToString();
                else
                    timestr = "1";

                labBonusValue.text = effectStr;
                labBonusTime.text = timestr + TableTextBlo.GetText("minute");

                wantedBossRoot.transform.localPosition = new Vector3(0, 54, 0);
            }
            else
            {
                wantedBossRoot.transform.localPosition = new Vector3(0, -50, 0);
            }

            if (bossInfo != null)
            {
                wantedBossRoot.SetActive(true);
                string heroId = DictWantedBlo.GetWantedLeaderId(bossInfo.wantedId);
                TableHero herodata = Dict.Blo.DictHeroBlo.GetTableHero(heroId);

                bossIconRoot.transform.DestroyChildren();
                GameObject icon = Instantiate(heroIconPref);
                Destroy(icon.GetComponent<BoxCollider>());
                icon.transform.parent = bossIconRoot.transform;
                icon.transform.localPosition = Vector3.zero;
                icon.transform.localScale = Vector3.one;
                HeroIcon bossIcon = icon.GetComponent<HeroIcon>();
                bossIcon.SetHeroWithId(heroId);
                bossIcon.levelBg.SetActive(true);
                bossIcon.heroLevel.text = bossInfo.level.ToString();

                labBossName.text = TableTextBlo.GetText(herodata.name);
            }

            GetComponent<UITweener>().ResetToBeginning();
            GetComponent<UITweener>().PlayForward();
        }

        void Update()
        {
            if(bossInfo != null)
            {
                long curTime = ServerTimerTool.CurrentUTCJavaTime();
                if (curTime >= bossInfo.runTime)
                {
                    wantedBossRoot.SetActive(false);
                    bossInfo = null;
                }
            }

            if (cookEffectList != null && cookEffectList.Count > 0)
            {
                long curTime = ServerTimerTool.CurrentUTCJavaTime();
                if (curTime >= cookEffectList[0].effectTime)
                {
                    cookBonusRoot.SetActive(false);
                    wantedBossRoot.transform.localPosition = new Vector3(0, -50, 0);
                    cookEffectList.RemoveAt(0);
                    return;
                }

                //每隔一分钟刷新一下显示时间
                delateTime += Time.deltaTime;
                if (delateTime >= 60)
                {
                    delateTime = 0;

                    System.DateTime time1 = ServerTimerTool.Java2CSTime(cookEffectList[0].effectTime);
                    System.DateTime time2 = ServerTimerTool.Java2CSTime(curTime);
                    System.TimeSpan time = time1.Subtract(time2);
                    string timestr = "";
                    if (time.Minutes > 0)
                        timestr = time.Minutes.ToString();
                    else
                        timestr = "1";

                    labBonusTime.text = timestr + TableTextBlo.GetText("minute");
                }
            }
        }
    }
}

